﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Profile;
using DataAccess;
using PersianDateControls;
using SubSonic;


namespace Business
{
    public class ProjectStatusTaskType
    {
        #region Properties

        private string _StepName;
        public string StepName
        {
            get { return _StepName; }
            set { _StepName = value; }
        }

        private int _StepPriority;
        public int StepPriority
        {
            get { return _StepPriority; }
            set { _StepPriority = value; }
        }

        private int _DeadlineDays;
        public int DeadlineDays
        {
            get { return _DeadlineDays; }
            set { _DeadlineDays = value; }
        }

        private int _PreviousStep;
        public int PreviousStep
        {
            get { return _PreviousStep; }
            set { _PreviousStep = value; }
        }

        private Guid _ContractId;
        public Guid ContractId
        {
            get { return _ContractId; }
            set { _ContractId = value; }
        }

        private bool _HasDeadline;
        public bool HasDeadline
        {
            get { return _HasDeadline; }
            set { _HasDeadline = value; }
        }

        private Guid _AdvisorStepId;
        public Guid AdvisorStepId
        {
            get { return _AdvisorStepId; }
            set { _AdvisorStepId = value; }
        }

        private Guid _SurveyorStepId;
        public Guid SurveyorStepId
        {
            get { return _SurveyorStepId; }
            set { _SurveyorStepId = value; }
        }

        private string _Status;
        public string Status
        {
            get { return _Status; }
            set { _Status = value; }
        }

        private DateTime _DateDone;
        public DateTime DateDone
        {
            get { return _DateDone; }
            set { _DateDone = value; }
        }

        private int _Latency;
        public int Latency
        {
            get { return _Latency; }
            set { _Latency = value; }
        }

        private string _DateDoneFarsi;
        public string DateDoneFarsi
        {
            get { return _DateDoneFarsi; }
            set { _DateDoneFarsi = value; }
        }

        private Guid _TaskId;
        public Guid TaskId
        {
            get { return _TaskId; }
            set { _TaskId = value; }
        }

        private bool _IsDone;
        public bool IsDone
        {
            get { return _IsDone; }
            set
            {
                _IsDone = value;
            }
        }

        private InputGuyType.InputGuy _InputGuy;
        public InputGuyType.InputGuy InputGuy
        {
            get { return _InputGuy; }
            set { _InputGuy = value; }
        }

        private bool _HasRepeat;
        public bool HasRepeat
        {
            get { return _HasRepeat; }
            set
            {
                _HasRepeat = value;
            }
        }

        private int _RepeatCount;
        public int RepeatCount
        {
            get { return _RepeatCount; }
            set
            {
                _RepeatCount = value;
            }
        }


        
        #endregion


        public ProjectStatusTaskType(TblContract contract , TblTask task  )
        {
            bool contractType = Convert.ToBoolean(contract.ContractType);
            if(contractType)
            {
                AdvisorConstructor(task);
            }
            else
            {
                SurvayorConstructor(task);
            }
            CommonTasks(task);
        }

        public ProjectStatusTaskType()
        {}


        private void CommonTasks(TblTask task)
        {
            ContractId = new Guid( Convert.ToString( task.ContractId ) );
            Status = task.Status;
            DateDone = Convert.ToDateTime( task.DateX );
            Latency = Convert.ToInt32( task.DelayDays );
            DateDoneFarsi = Convertor.ToPersianDate( DateDone );
            TaskId = task.Id;
            IsDone = Convert.ToBoolean( task.IsDonei );
            
            if ( System.Web.Security.Roles.IsUserInRole( task.UserName, "advisor" ) )
            {
                InputGuy = InputGuyType.InputGuy.Advisor;
            }

            if (System.Web.Security.Roles.IsUserInRole(task.UserName, "bonyadshahrestan"))
            {
                InputGuy = InputGuyType.InputGuy.BonyadeShahrestan;
            }

            if (System.Web.Security.Roles.IsUserInRole(task.UserName, "employer"))
            {
                InputGuy = InputGuyType.InputGuy.Employer;
            }

            if (System.Web.Security.Roles.IsUserInRole(task.UserName, "fourthfactor"))
            {
                InputGuy = InputGuyType.InputGuy.FourthFactor;
            }
        }



        private void SurvayorConstructor(TblTask task)
        {
            TblStepsSurveyorCollection stepSurveyorCollection = new TblStepsSurveyorCollection();
            
            stepSurveyorCollection.LoadAndCloseReader(TblStepsSurveyor.FetchByParameter(TblStepsSurveyor.Columns.Id, Comparison.Equals, task.SurveyorStepId));
            if ( stepSurveyorCollection.Count > 0 )
            {
                var step = stepSurveyorCollection[0];
                StepName = String.Format("{0}. {1}", step.Priority, step.Name);
                StepPriority = Convert.ToInt32(step.Priority);
                DeadlineDays = Convert.ToInt32(step.DeadLineDays);
                PreviousStep = Convert.ToInt32(step.PreviousStep);
                HasDeadline = Convert.ToBoolean(step.HasDeadLine);
                SurveyorStepId = step.Id;
                HasRepeat = Convert.ToBoolean(step.HasRepeat);
                RepeatCount = Convert.ToInt32(step.RepeatCount);
            }
        }

        private void AdvisorConstructor(TblTask task)
        {
            TblStepsAdvisorCollection stepAdvisorCollection = new TblStepsAdvisorCollection();
            stepAdvisorCollection.LoadAndCloseReader(TblStepsAdvisor.FetchByParameter(TblStepsAdvisor.Columns.Id, Comparison.Equals, task.AdvisorStepId));

            if ( stepAdvisorCollection.Count > 0 )
            {
                TblStepsAdvisor step = stepAdvisorCollection[0];
                StepName = String.Format("{0}. {1}", step.Priority, step.Name);
                StepPriority = Convert.ToInt32(step.Priority);
                DeadlineDays = Convert.ToInt32(step.DeadLineDays);
                PreviousStep = Convert.ToInt32(step.PreviousStep);
                HasDeadline = Convert.ToBoolean(step.HasDeadLine);
                AdvisorStepId = step.Id;
                HasRepeat = Convert.ToBoolean(step.HasRepeat);
                RepeatCount = Convert.ToInt32(step.RepeatCount);
            }
        }
    }
}
